package com.augurit.gzcgwsp.webservice.service.impl;

import java.text.ParseException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.ljzyz.service.IYwYnBusinfoService;
import com.augurit.gzcgwsp.ljzyz.web.form.YwYnBusinfoForm;
import com.augurit.gzcgwsp.webservice.service.ICarDataService;

/**
 * 保存实体接口
 * @author Administrator
 *
 */
@Service
@Transactional
public class CarDataServiceImpl implements ICarDataService{
	@Autowired
	private IYwYnBusinfoService ywYnBusinfoService;
	
	@Override
	public String saveCar(String  xml) {
		YwYnBusinfoForm form= buildForm(xml);
		String result=ywYnBusinfoService.saveCarInfo(form);
		return result;
	}	
	
	/**
	 * 构建xml文件
	 * @param xml
	 * @return
	 */
	private YwYnBusinfoForm buildForm(String xml){
		    YwYnBusinfoForm busInfo=new YwYnBusinfoForm();
		try {
			Document document=DocumentHelper.parseText(xml);
			Element root = document.getRootElement();
			String carId=getElementValue(root, "carId");
			String cjh=getElementValue(root, "cjh");
			String fdjh=getElementValue(root, "fdjh");
			String clbh=getElementValue(root, "clbh");
			String cphm=getElementValue(root, "cphm");
			String qydj=getElementValue(root, "qydj");
			String gsmc=getElementValue(root, "gsmc");
			String yxkssj=getElementValue(root, "yxkssj");
			String yxjssj=getElementValue(root, "yxjssj");
			busInfo.setId(Long.valueOf(carId));
			busInfo.setCjh(cjh);
			busInfo.setGsmc(gsmc);
			busInfo.setFdjh(fdjh);
			busInfo.setCphm(cphm);
			busInfo.setCphm(cphm);
			busInfo.setClbh(clbh);
			busInfo.setFdjh(fdjh);
			try {
				busInfo.setYxksrq(DateUtilsEX.dateFormate(yxkssj, "yyyy-MM-dd"));
				busInfo.setYxjsrq(DateUtilsEX.dateFormate(yxjssj, "yyyy-MM-dd"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		return busInfo;
	}
	
	public static List<Element> getElements(Element ele, String name){
		List nodes = ele.elements(name);   
		return nodes;
	}
	
	public  String getElementValue(Element ele,String name){
		List<Element> list=getElements(ele, name);
		String value="";
		for (Iterator it = list.iterator(); it.hasNext();) {   
			Element elm = (Element) it.next();  
			value=elm.getText().toString();
		}  
		return value;
	}
	

	
	
}
